//
//  LoginViewController.m
//  模拟-百思不得姐
//
//  Created by 谭浔明 on 16/8/6.
//  Copyright © 2016年 chunyu. All rights reserved.
//

#import "LoginViewController.h"
#import "RetrievePasswordViewController.h"
#import "UMSocial.h"
#import "zhuceeViewController.h"



#import "EMClient.h"



@interface LoginViewController () <UMSocialUIDelegate, UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (nonatomic,strong) NSArray *array;

@end

@implementation LoginViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    


    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(fangfafa:) name:@"loggg" object:nil];
    
    
    
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{
    
    //[self.textName resignFirstResponder];
    
    //[self.textSummary resignFirstResponder];
    [self.view endEditing:YES];
    
}



-(void)fangfafa:(NSNotification*)notification{
    
    NSArray *array=[[NSArray alloc]initWithArray:notification.object];
    NSLog(@"%@",array);
    _userName.text=array[0];
    _password.text=array[1];
    
}



//取消
- (IBAction)cuoBtn:(UIButton *)sender {
    
    
    NSLog(@"bde");
    
    [self dismissViewControllerAnimated:YES completion:nil];
 }

//注册按钮
- (IBAction)zhuceeee:(UIButton *)sender {
    
    
    zhuceeViewController *zhuceVC=[[zhuceeViewController alloc]init];
    
    [self presentViewController:zhuceVC animated:YES completion:nil];

    
    
}

 
//找回密码
- (IBAction)RetrievepasswordBtn:(UIButton *)sender {

    RetrievePasswordViewController *RPVC=[[RetrievePasswordViewController alloc]init];
    
    [self.navigationController pushViewController:RPVC animated:YES];


}

//QQ
- (IBAction)QQBtn:(UIButton *)sender {
    
    // Sns平台类，用`platformName`作为标识，指定显示名称、显示的图片，点击之后的响应。 根据平台名返回平台对象
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQzone];
    
    // loginClickHandler 处理点击事件的
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        // responseCode 发送结果的     成功
        if (response.responseCode == UMSResponseCodeSuccess) {
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
            [[EMClient sharedClient] registerWithUsername:snsAccount.usid password:snsAccount.usid];
            [[EMClient sharedClient] loginWithUsername:snsAccount.usid password:snsAccount.usid];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:nil];
            [self dismissViewControllerAnimated:NO completion:nil];
        }});
}


//微博
- (IBAction)weiboBtn:(UIButton *)sender {
    
    
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        // responseCode 发送结果的     成功
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
            [[EMClient sharedClient] registerWithUsername:snsAccount.usid password:snsAccount.usid];
            [[EMClient sharedClient] loginWithUsername:snsAccount.usid password:snsAccount.usid];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:nil];
            [self dismissViewControllerAnimated:NO completion:nil];
        }});

    
}


//腾讯微博
- (IBAction)tengxunBtn:(UIButton *)sender {
    
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToTencent];
    
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
            [[EMClient sharedClient] registerWithUsername:snsAccount.usid password:snsAccount.usid];
            [[EMClient sharedClient] loginWithUsername:snsAccount.usid password:snsAccount.usid];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:nil];
            [self dismissViewControllerAnimated:NO completion:nil];
        }});
}

//登录
- (IBAction)logBtn:(UIButton *)sender {
    
    
    EMError *error = [[EMClient sharedClient] loginWithUsername:_userName.text password:_password.text];
    
    
    if (!error) {
        [[EMClient sharedClient].options setIsAutoLogin:YES];
        // 登录成功后，通知各页面对自身进行修改
         
        [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:nil];
      
 

        
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"登录成功" message:@"恭喜你成功登录" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *Aciton = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        [alertVC addAction:Aciton];
        [self presentViewController:alertVC animated:YES completion:nil];
        } else {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"登录失败" message:@"用户名密码错误或者重复登录" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *Aciton = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
        
        
        [alertVC addAction:Aciton];
        
        
        [self presentViewController:alertVC animated:YES completion:nil];
    }
    
    
    
}







-(void)tapPage3
{
    
    
    [self dismissViewControllerAnimated:YES completion:nil];


}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.tag == 10) {
        [[self.view viewWithTag:11] becomeFirstResponder];
    } else {
        [self logBtn:nil];
    }
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end
